package com.uhylab.uhuvinetest.dao;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.uhylab.uhuvinetest.UHApplication;

/**
 * Created by xyb on 16/1/6 at UHylab
 *
 * @version 1.0
 */
public class DaoSingleton {
    private static final String DATABASE_NAME = "uhydao.db";

    private volatile static DaoSingleton instance;

    private SQLiteDatabase db;
    private DaoMaster daoMaster;
    private DaoSession daoSession;
    private Context context;
    private DaoMaster.DevOpenHelper helper;
    private UserDataEntityDao userDataEntityDao;
    private LocalHistoryDao localHistoryDao;
    private CloudHistoryDao cloudHistoryDao;
    private UserDisHistoryEntityDao mUserDisHistoryEntityDao;

    private DaoSingleton() {
        context = UHApplication.gainContext();
    }

    public static DaoSingleton getInstance() {
        if (instance == null) {
            synchronized (DaoSingleton.class) {
                if (instance == null) {
                    instance = new DaoSingleton();

                }
            }
        }
        return instance;
    }

    public DaoMaster.DevOpenHelper getHelper() {
        if (helper == null) {
            helper = new DaoMaster.DevOpenHelper(context, DATABASE_NAME, null);
        }
        return helper;
    }

    private SQLiteDatabase getDb() {
        if (db == null) {
            db = getHelper().getWritableDatabase();

        }
        return db;
    }

    private DaoMaster getDaoMaster() {
        if (daoMaster == null) {
            daoMaster = new DaoMaster(getDb());
        }
        return daoMaster;
    }

    public DaoSession getDaoSession() {
        if (daoSession == null) {
            daoSession = getDaoMaster().newSession();
        }
        return daoSession;
    }

    public UserDataEntityDao getUserDataEntityDao() {
        if (userDataEntityDao == null) {
            userDataEntityDao = getDaoSession().getUserDataEntityDao();
        }
        return userDataEntityDao;
    }

    public LocalHistoryDao getLocalHistoryDao() {
        if (localHistoryDao == null) {
            localHistoryDao = getDaoSession().getLocalHistoryDao();
        }
        return localHistoryDao;
    }

    public CloudHistoryDao getCloudHistoryDao() {
        if (cloudHistoryDao == null) {
            cloudHistoryDao = getDaoSession().getCloudHistoryDao();
        }
        return cloudHistoryDao;
    }

    public UserDisHistoryEntityDao getUserDisHistoryEntityDao() {
        if (mUserDisHistoryEntityDao == null) {
            mUserDisHistoryEntityDao = getDaoSession().getUserDisHistoryEntityDao();
        }
        return mUserDisHistoryEntityDao;
    }
}
